﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using DAL;
using System.Configuration;
using System.Data;
using System.Drawing;

public partial class Page_SystemFramework : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack || (HiddenField2.Value != null && HiddenField2.Value == "1"))
        {            
            HiddenField2.Value = "";
            SetTreeView();
            if (HiddenField1.Value == null || HiddenField1.Value == "")//没有查询id
            {
                Literal1.Text = "<span style=\"font-size: 24px;font-weight: bold;\">请选择左边相关层级进行管理</span>";
                Button1.Visible = false;
                divShow.Style.Add("display", "none");
            }
            else
            {
                SystemFrameworkData sfd = SystemFrameworkDataDBOption.SelectByID(Guid.Parse(HiddenField1.Value));
                DataTable dt = SystemFrameworkDataDBOption.SelectByLevel(sfd.ID, sfd.FrameLevel + 1);
                Literal1.Text = "<span style=\"font-size: 16px;font-weight: bold;\">当前位置：</span>" + "<span style=\"font-size: 24px;font-weight: bold;\">" + sfd.Name + "</span>";
                TextBox1.Text = sfd.Name;
                TextBox2.Text = sfd.Contact;
                TextBox3.Text = sfd.Organization;
                TextBox4.Text = sfd.Address;
                TextBox5.Text = sfd.Telephone;
                TextBox6.Text = sfd.Remark;
                TextBox7.Text = sfd.Creator;
                TextBox8.Text = sfd.CreateTime.ToString();
                TextBox9.Text = sfd.UpdateTime.ToString();
                TextBox10.Text = sfd.MeasurementCountLimit.ToString();
                TextBox11.Text = sfd.CurrentMeasurementCount.ToString();
                Button1.Attributes.Add("onclick", "javascript:showdialog('" + HiddenField1.Value + "','')");
                Button2.Attributes.Add("onclick", "javascript:showdialog('" + sfd.SuperiorID.ToString() + "','" + sfd.ID.ToString() + "')");
                if (sfd.FrameLevel > int.Parse(ConfigurationManager.AppSettings.Get("FrameLevelLimit")))
                    Button1.Enabled = false;
            }
        }        
    }
    void SetTreeView()
    {
        TreeView1.Nodes.Clear();
        MembershipUser user = Membership.GetUser();
        Guid userid = (Guid)user.ProviderUserKey;
        UserExData f = UserExDataDBOption.SelectByUserID(userid);
        if (f != null)
        {
            SystemFrameworkData sfd = SystemFrameworkDataDBOption.SelectByID(f.UserLevelID);
            TreeNode xt_tn = new TreeNode(sfd.Name,sfd.ID.ToString());
            AddFrameLevel(xt_tn, sfd.FrameLevel + 1, sfd.ID);
            TreeView1.Nodes.Add(xt_tn);
        }
        else
        {
            if (user.UserName == "wangf0228")//超级管理员
            {

                TreeNode xt_tn = new TreeNode("系统平台", Guid.Empty.ToString());//, "", "", "~/Page/SystemFramework.aspx?id="+Guid.Empty.ToString(), "_parent");
                AddFrameLevel(xt_tn, 1, Guid.Empty);
                TreeView1.Nodes.Add(xt_tn);
            }
        }
        TreeView1.ExpandAll();
    }
    void AddFrameLevel(TreeNode tn,int Level,Guid SuperiorID)
    {
        if (Level > int.Parse(ConfigurationManager.AppSettings.Get("FrameLevelLimit")))
            return;        
        DataTable dt = SystemFrameworkDataDBOption.SelectByLevel(SuperiorID,Level);            
        foreach (DataRow dr in dt.Rows)
        {
            SystemFrameworkData sfd = SystemFrameworkDataFactory.Construct(dr);
            TreeNode tn1 = new TreeNode(sfd.Name, sfd.ID.ToString());//, "", "", url+sfd.ID.ToString(), "_parent");
            AddFrameLevel(tn1, Level + 1,sfd.ID);
            tn.ChildNodes.Add(tn1);
        }
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (TreeView1.SelectedNode != null)
        {
            string id = TreeView1.SelectedNode.Value;
            HiddenField1.Value = id;
            if (id == null || id == "")//没有查询id
            {
                Literal1.Text = "<span style=\"font-size: 24px;font-weight: bold;\">请选择左边相关层级进行管理</span>";
                Button1.Visible = false;
                divShow.Style.Add("display", "none");
            }
            else
            {                
                Button1.Visible = true;
                DataTable dt;
                divShow.Style.Add("display", "block");
                if (Guid.Parse(id) == Guid.Empty)
                {
                    divShow.Style.Add("display", "none");
                    Literal1.Text = "<span style=\"font-size: 16px;font-weight: bold;\">当前位置：</span>"+"<span style=\"font-size: 24px;font-weight: bold;\">系统平台</span>";
                    Button1.Attributes.Add("onclick", "javascript:showdialog('" + id + "',''" + ")");
                }
                else
                {
                    SystemFrameworkData sfd = SystemFrameworkDataDBOption.SelectByID(Guid.Parse(id));
                    dt = SystemFrameworkDataDBOption.SelectByLevel(sfd.ID, sfd.FrameLevel + 1);
                    Literal1.Text = "<span style=\"font-size: 16px;font-weight: bold;\">当前位置：</span>" + "<span style=\"font-size: 24px;font-weight: bold;\">"+sfd.Name+"</span>";
                    TextBox1.Text = sfd.Name;
                    TextBox2.Text = sfd.Contact;
                    TextBox3.Text = sfd.Organization;
                    TextBox4.Text = sfd.Address;
                    TextBox5.Text = sfd.Telephone;
                    TextBox6.Text = sfd.Remark;
                    TextBox7.Text = sfd.Creator;
                    TextBox8.Text = sfd.CreateTime.ToString();
                    TextBox9.Text = sfd.UpdateTime.ToString();
                    TextBox10.Text = sfd.MeasurementCountLimit.ToString();
                    TextBox11.Text = sfd.CurrentMeasurementCount.ToString();
                    Button1.Attributes.Add("onclick", "javascript:showdialog('" + id + "','')");
                    Button2.Attributes.Add("onclick", "javascript:showdialog('" + sfd.SuperiorID.ToString() + "','"+sfd.ID.ToString()+"')");
                    if (sfd.FrameLevel > int.Parse(ConfigurationManager.AppSettings.Get("FrameLevelLimit")))
                        Button1.Enabled = false;
                }
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        SystemFrameworkDataDBOption.DeleteByID(Guid.Parse(HiddenField1.Value));
        HiddenField1.Value = "";
        SetTreeView();
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('删除成功');</script>");
        Literal1.Text = "<span style=\"font-size: 24px;font-weight: bold;\">请选择左边相关层级进行管理</span>";
        Button1.Visible = false;
        divShow.Style.Add("display", "none");
    }
}
